package com.cjh.springboot.Service;

import com.cjh.springboot.dao.DiscussPostMapper;
import com.cjh.springboot.dao.UserMapper;
import com.cjh.springboot.entity.DiscussPost;
import com.cjh.springboot.entity.User;
import com.cjh.springboot.util.SensitiveFiter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class DiscussPostService {
    @Autowired
    private DiscussPostMapper discussionPostMapper;

    @Autowired
    private SensitiveFiter sensitiveFilter;
    @Autowired
    private UserMapper usermapper;

    public List<DiscussPost> findalldiscusspost(int userId, int offset, int limit){
        return discussionPostMapper.selectDiscussPost(userId,offset,limit);
    }
    public int findDiscussPostRows(int userId) {
        return discussionPostMapper.selectDiscussPostRows(userId);
    }


    public int addDiscussPost(DiscussPost post) {
        if (post == null) {
            throw new IllegalArgumentException("参数不能为空!");
        }

        // 转义HTML标记
        post.setTitle(HtmlUtils.htmlEscape(post.getTitle()));
        post.setContent(HtmlUtils.htmlEscape(post.getContent()));
        // 过滤敏感词
        post.setTitle(sensitiveFilter.filter(post.getTitle()));
        post.setContent(sensitiveFilter.filter(post.getContent()));

        return discussionPostMapper.insertDiscussPost(post);
    }

    public DiscussPost findDiscussPostByid(int id){
       return  discussionPostMapper.selectDiscussPostById(id);
    }

    public int updateCommentCount(int id, int commentCount) {
        return discussionPostMapper.updateCommentCount(id, commentCount);
    }

}

